package com.boco.modules.fdoc.web.organization;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.boco.common.enums.ApiStatusEnum;
import com.boco.common.json.BaseJsonVo;
import com.boco.common.persistence.Page;
import com.boco.common.utils.JsonUtils;
import com.boco.modules.fdoc.model.DoctorEntity;
import com.boco.modules.fdoc.model.DoctorUserEntity;
import com.boco.modules.fdoc.model.HospitalEntity;
import com.boco.modules.fdoc.model.expert.ExpertAccountEntity;
import com.boco.modules.fdoc.model.expert.ExpertInformationEntity;
import com.boco.modules.fdoc.service.DepartmentService;
import com.boco.modules.fdoc.service.DocService;
import com.boco.modules.fdoc.service.DocUserService;
import com.boco.modules.fdoc.service.ExpertService;
import com.boco.modules.fdoc.service.HospitalService;
import com.boco.modules.fdoc.vo.DoctorDetailVo;
import com.boco.modules.fdoc.vo.ExpertVo;

/**
 * Description 专家信息管理
 * @author lzz
 * @date 2017年9月26日 上午9:17:12
 */
@Controller
@RequestMapping(value="/expert",produces = "application/json;charset=UTF-8")
public class ExpertController {
	
	@Resource
	HospitalService hospitalService;
	@Resource
	DepartmentService deptService;
	@Resource
	ExpertService expertService;
	
	/**
	 * 跳转专业首页
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/showPage",method = RequestMethod.GET)
	public String showPage(HttpServletRequest request, Model model){
		//获取医院列表
		List<HospitalEntity> hospitalList = hospitalService.getHospitalList();
		//设置作用域对象
		model.addAttribute("hospitalList", hospitalList);
		model.addAttribute("hospitalList2",JsonUtils.getJson(hospitalList));
		return "/expert/expertInfo";
	}
	
	/**
	 * 根据条件获取专家列表
	 * @param request
	 * @param model
	 * @param orgId 医院id
	 * @param expertName 专家姓名
	 * @param pageIndex 页码
	 * @param pageSize 页面大小
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/getExpertList",method = RequestMethod.GET)
	public String getExpertList(HttpServletRequest request, Model model,String orgId,
			String expertName,Integer pageIndex,Integer pageSize){
		ExpertVo expertVo=new ExpertVo();
		Page<ExpertVo> page=new Page<ExpertVo>(pageIndex, pageSize);
		expertVo.setExpertName(expertName);
		expertVo.setOrgId(orgId);
		int count = expertService.getExpertCount(expertVo);
		expertVo.setPage(page);
		List<ExpertVo> list=expertService.getExpertList(expertVo);
		return JsonUtils.getJson(BaseJsonVo.pageList(list, count));

	}
	
	/**
	 * 跳转新增专家页面
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/toAddExpert",method = RequestMethod.GET)
	public String toAddExpert(HttpServletRequest request, Model model){
		//获取医院列表
		List<HospitalEntity> hospitalList = hospitalService.getHospitalList();
		//设置作用域对象
		model.addAttribute("hospitalList", hospitalList);
		return "/expert/addExpert";
	}
	
	/**
	 * 保存专家信息
	 * @param request
	 * @param model
	 * @param expertName 专家姓名
	 * @param phoneNumber 专家电话号码
	 * @param orgId 医院id
	 * @param title 职称
	 * @param sex 性别
	 * @param specialty 特长
	 * @param introduction 介绍
	 * @param department 科室
	 * @param userName 专家账号
	 * @param password 专家密码
	 * @param idCard 专家身份证
	 * @param qualificationNumber 资格证号
	 * @param practisingCertificateNumber 执业证号
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/saveExpert",method = RequestMethod.POST)
	public String saveExpert(HttpServletRequest request, Model model,String expertName,String phoneNumber,
			String orgId,String title,String sex,String specialty,String introduction,String department,
			String userName,String password,String idCard,String qualificationNumber,String practisingCertificateNumber){
		
		ExpertInformationEntity informationEntity=new ExpertInformationEntity();
		ExpertAccountEntity accountEntity=new ExpertAccountEntity();
		
		String id =UUID.randomUUID().toString().replace("-", "");
		String accountId=UUID.randomUUID().toString().replace("-", "");
		//专家信息实体
		informationEntity.setId(id);
		informationEntity.setExpertName(expertName);
		informationEntity.setSex(sex);
		informationEntity.setTitle(title);
		informationEntity.setIntroduction(introduction);
		informationEntity.setSpecialty(specialty);
		informationEntity.setPhoneNumber(phoneNumber);
		informationEntity.setOrgId(orgId);
		informationEntity.setDepartment(department);
		informationEntity.setCreateTime(new Date());
		informationEntity.setUpdateTime(new Date());
		informationEntity.setIdCard(idCard);
		informationEntity.setQualificationNumber(qualificationNumber);
		informationEntity.setPractisingCertificateNumber(practisingCertificateNumber);
		//装甲账号实体
		accountEntity.setExpertAccountId(accountId);
		accountEntity.setUserName(userName);
		accountEntity.setPassword(password);
		accountEntity.setExpertId(id);
		accountEntity.setCreateTime(new Date());
		accountEntity.setUpdateTime(new Date());
		
		int flag=expertService.saveExpertAndAccount(informationEntity,accountEntity);
		
		if(flag>0){
			return JsonUtils.getJsonFormat(BaseJsonVo.success(null));
		}else if(flag==-1){
			return JsonUtils.getJsonFormat(BaseJsonVo.empty(ApiStatusEnum.FAIL.getKey(), "此身份证医生已经存在，请确认后修改。"));
		}else {
			return JsonUtils.getJsonFormat(BaseJsonVo.empty(ApiStatusEnum.FAIL.getKey(), ApiStatusEnum.FAIL.getValue()));
		}
	}
	
	/**
	 * 根据专家id 获取专家信息
	 * @param request
	 * @param model
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/getExpertbyId",method = RequestMethod.GET)
	public String getExpertbyId(HttpServletRequest request, Model model,String id){
		ExpertVo expertVo=expertService.getExpertById(id);
		model.addAttribute("expertVo", expertVo);
		return "/expert/expertDetail";
	}

	/**
	 * 跳转专家修改页面
	 * @param request
	 * @param model
	 * @param id 专家id
	 * @return
	 */
	@RequestMapping(value="/toUpdateExpert",method = RequestMethod.GET)
	public String toUpdateExpert(HttpServletRequest request, Model model,String id){
		ExpertVo expertVo=expertService.getExpertById(id);
		model.addAttribute("expertVo", expertVo);
		//获取医院列表
		List<HospitalEntity> hospitalList = hospitalService.getHospitalList();
		model.addAttribute("hospitalList", hospitalList);
		return "/expert/updateExpert";
		
	}
	
	/**
	 * 修改专家
	 * @param request
	 * @param model
	 * @param id 专家id
	 * @param expertAccountId 专家账号id
	 * @param expertName 专家姓名
	 * @param phoneNumber 专家电话
	 * @param orgId 医院id
 	 * @param title 职称
	 * @param sex 性别
	 * @param specialty 特长
	 * @param introduction 介绍
	 * @param department 科室
	 * @param userName 该专家账号
	 * @param password 该专家密码
	 * @param idCard 身份证号
	 * @param qualificationNumber 资格证号
	 * @param practisingCertificateNumber 执业证号
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/updateExpert",method = RequestMethod.POST)
	public String updateExpert(HttpServletRequest request, Model model,String id,String expertAccountId,String expertName,String phoneNumber,
			String orgId,String title,String sex,String specialty,String introduction,String department,
			String userName,String password,String idCard,String qualificationNumber,String practisingCertificateNumber){
		
		ExpertInformationEntity informationEntity=new ExpertInformationEntity();
		ExpertAccountEntity accountEntity=new ExpertAccountEntity();
		//专家信息实体
		informationEntity.setId(id);
		informationEntity.setExpertName(expertName);
		informationEntity.setSex(sex);
		informationEntity.setTitle(title);
		informationEntity.setIntroduction(introduction);
		informationEntity.setSpecialty(specialty);
		informationEntity.setPhoneNumber(phoneNumber);
		informationEntity.setOrgId(orgId);
		informationEntity.setDepartment(department);
		informationEntity.setUpdateTime(new Date());
		informationEntity.setIdCard(idCard);
		informationEntity.setQualificationNumber(qualificationNumber);
		informationEntity.setPractisingCertificateNumber(practisingCertificateNumber);
		//装甲账号实体
		if(expertAccountId!=null){
			accountEntity.setExpertAccountId(expertAccountId);
		}
		accountEntity.setUserName(userName);
		accountEntity.setPassword(password);
		accountEntity.setExpertId(id);
		accountEntity.setUpdateTime(new Date());
		
		int flag=expertService.updateExpert(informationEntity,accountEntity);
		if(flag>0){
			return JsonUtils.getJsonFormat(BaseJsonVo.success(null));
		}else if(flag==-1){
			return JsonUtils.getJsonFormat(BaseJsonVo.empty(ApiStatusEnum.FAIL.getKey(), "此身份证已有医生已经存在，请确认后修改。"));
		}else{
			return JsonUtils.getJsonFormat(BaseJsonVo.empty(ApiStatusEnum.FAIL.getKey(), ApiStatusEnum.FAIL.getValue()));
		}
	}
}
